<template>
  <ele-page flex-table>
    <ele-card flex-table
              :body-style="{ padding: '0px', overflow: 'hidden' }">
      <ele-split-panel flex-table
                       size="500px"
                       allow-collapse
                       :custom-style="{ borderWidth: '0 1px 0 0', padding: '0' }"
                       :body-style="{ padding: '16px 16px 0 0', overflow: 'hidden' }"
                       :style="{ height: '100%', overflow: 'visible' }">
        <div style="padding: 16px 16px;">
          <type-search @search="reload"/>
          <div>
            <ele-pro-table
              ref="tableRef"
              row-key="id"
              :columns="columns"
              :datasource="datasource"
              :show-overflow-tooltip="true"
              highlight-current-row
              cache-key="cmsTypeTable"
              :tools="false"
              @current-change="handleCurrentChange"
            >
              <template #toolbar>
                <el-button
                  type="primary"
                  class="ele-btn-icon"
                  :icon="PlusOutlined"
                  @click="openAdd()"
                >
                  新建
                </el-button>
                <el-button
                  type="warning"
                  class="ele-btn-icon"
                  :icon="EditOutlined"
                  @click="openEdit()"
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  class="ele-btn-icon"
                  :icon="DeleteOutlined"
                  @click="removeBatch()"
                >
                  删除
                </el-button>
              </template>
              <template #action="{ row }">
                <el-link type="primary" :underline="false" @click="openEdit(row)">
                  修改
                </el-link>
                <el-divider direction="vertical"/>
                <el-link type="danger" :underline="false" @click="removeBatch(row)">
                  删除
                </el-link>
              </template>
              <template #status="{ row }">
                <el-switch
                  size="small"
                  :model-value="row.status == 0"
                  @change="(checked) => editStatus(checked, row)"
                />
              </template>
              <template #name="{row}">
                <el-tooltip
                  class="box-item"
                  effect="light"
                  :content="'标识:'+row.identityKey"
                  placement="right"
                >
                  {{ row.name }}
                </el-tooltip>
              </template>
            </ele-pro-table>
          </div>
        </div>
        <template #body>
          <div>
            <nav-item-table :typeId="currentId"></nav-item-table>
          </div>
        </template>
      </ele-split-panel>
    </ele-card>
    <type-add v-model="showAdd" @done="reload"/>
    <type-edit v-model="showEdit" :data="current" @done="reload"/>
  </ele-page>
</template>

<script setup>
import {ref} from 'vue';
import {
  PlusOutlined,
  DeleteOutlined,
  DownloadOutlined,
  EditOutlined
} from '@/components/icons';
import {ElMessageBox} from 'element-plus/es';
import {EleMessage} from 'ele-admin-plus/es';
import TypeSearch from './components/nav-type-search.vue';
import TypeAdd from './components/nav-type-add.vue';
import TypeEdit from './components/nav-type-edit.vue';
import NavItemTable from './components/nav-item-table.vue';
import {pageTypes, removeType, removeTypes, updateTypesStatus} from '@/api/cms/nav/type';


/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    prop: 'name',
    label: '名称',
    align: 'center',
    minWidth: 120,
    slot: 'name',
  },
  {
    prop: 'orderNum',
    label: '显示顺序',
    align: 'center',
    minWidth: 80
  },
  {
    prop: 'status',
    label: '状态',
    align: 'center',
    minWidth: 90,
    slot: 'status',
  }
]);

/** 表格选中数据 */
// const selections = ref({});

/** 当前编辑数据 */
const current = ref(null);
const currentId = ref(null);

/** 是否显示新增弹窗 */
const showAdd = ref(false);

/** 是否显示编辑弹窗 */
const showEdit = ref(false);

/** 表格数据源 */
const datasource = ({pages, where}) => {
  return pageTypes({...where, ...pages});
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};


const openAdd = () => {
  showAdd.value = true;
}

/** 打开编辑弹窗 */
const openEdit = () => {
  const row = current.value;
  if (Object.keys(row).length === 0) {
    EleMessage.warning('请选择需要编辑的数据');
  }
  current.value = row;
  showEdit.value = true;
};

/** 批量删除 */
const removeBatch = () => {
  const row = current.value;
  if (!row) {
    EleMessage.warning('请至少选择一条数据');
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除类别名称为"${row.name}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  ).then(() => {
    const loading = EleMessage.loading('请求中..');
    removeType(row.id)
      .then(() => {
        loading.close();
        EleMessage.success('删除成功');
        reload();
      })
      .catch((e) => {
        loading.close();
        EleMessage.error(e.message);
      });
  })
    .catch(() => {
    });
};


/** 修改类型状态 */
const editStatus = (checked, row) => {
  const status = checked ? '0' : '1';
  updateTypesStatus(row.id, status)
    .then((msg) => {
      row.status = status;
      EleMessage.success(msg);
    })
    .catch((e) => {
      EleMessage.error(e.message);
    });
};

//获取当前选中的值
const handleCurrentChange = (currentRow, oldCurrentRow) => {
  current.value = currentRow;//记录当前选中的值
  currentId.value = current.value.id;
}

/** 导出数据 */
// const exportData = () => {
//   const loading = EleMessage.loading('请求中..');
//   tableRef.value?.fetch?.(({where}) => {
//     exportTypes(where)
//       .then(() => {
//         loading.close();
//       })
//       .catch((e) => {
//         loading.close();
//         EleMessage.error(e.message);
//       });
//   });
// };
</script>
